//
//  WYRecommendController.m
//  CirclePush
//
//  Created by admin on 2018/4/9.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYRecommendController.h"
#import "WYRecommendCell.h"
#import "ZYHomeController.h"
@interface WYRecommendController ()
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)ZYHomeController *homeVC;
@property (nonatomic, strong) UIButton *searchButton;
@end

@implementation WYRecommendController
-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    
    
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    for (int i=0; i<3; i++) {
        [self.dataArray addObject:@""];
    }
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"WYRecommendCell" bundle:nil] forCellReuseIdentifier:@"WYRecommendCell"];
    
    
   
   // self.tableView.bounces = NO;
    self.homeVC = [[ZYHomeController alloc]init];
    self.tableView.tableHeaderView = self.homeVC.view;
    self.homeVC.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight+100);
    [self setupUI];
    
}
- (void)setupNavItems
{
    UIView *leftView = [[UIView alloc]init];
    //leftView.backgroundColor = [UIColor yellowColor];
    UILabel *nameLabel = [[UILabel alloc]init];
    [leftView addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.centerY.mas_equalTo(leftView);
    }];
    
    UIImageView *bottomView = [[UIImageView alloc]init];
    [leftView addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(nameLabel).offset(-2);
        make.left.equalTo(nameLabel.mas_right).offset(5);
        make.width.mas_equalTo(12);
        make.height.mas_equalTo(7);
    }];
    bottomView.image = [UIImage imageNamed:@"iocn_bottom_arrow"];
    nameLabel.text = @"北京市";
    nameLabel.textColor = [UIColor whiteColor];
    nameLabel.font = [UIFont systemFontOfSize:13];
    
    UIButton *tapButton = [[UIButton alloc]init];
    [leftView addSubview:tapButton];
    [tapButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.mas_equalTo(0);
    }];
    [tapButton addTarget:self action:@selector(onClickLeft) forControlEvents:UIControlEventTouchUpInside];
    //  tapButton.backgroundColor = [UIColor redColor];
    // leftView.frame = CGRectMake(0, 0, kScreenWidth, 44);
    //    UITapGestureRecognizer *ges = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickLeft)];
    //    [leftView addGestureRecognizer:ges];
    
    
    //    UIButton *leftButton = [[UIButton alloc]init];
    //    leftButton.frame = CGRectMake(0, 0, 80, 44);
    //    leftButton.backgroundColor = [UIColor redColor];
    //    leftButton.titleLabel.font = [UIFont systemFontOfSize:13];
    //    [leftButton setImage:[UIImage imageNamed:@"iocn_bottom_arrow"] forState:UIControlStateNormal];
    //    [leftButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -70)];
    //    [leftButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 30)];
    //    self.leftButton = leftButton;
    //    [leftButton setTitle:@"北京市" forState:UIControlStateNormal];
    //    [leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    //    [leftButton addTarget:self action:@selector(onClickLeft) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftView];
    //    UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"navLeft"] style:UIBarButtonItemStylePlain target:self action:@selector(onClickLeft)];
    self.navigationItem.leftBarButtonItem = leftButtonItem;
    
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"search-1"] style:UIBarButtonItemStylePlain target:self action:@selector(onClickRight)];
    rightButtonItem.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
    
    //    // 这里我就不适配了 (适配后 push/pop 时右边的黑影就会自动消失)
    //    self.searchButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 230, 30)];
    //    [self.searchButton setTitle:@"搜索职位/公司/商区" forState:UIControlStateNormal];
    //    self.searchButton.titleLabel.font = [UIFont systemFontOfSize:13];
    //    [self.searchButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    //    [self.searchButton setBackgroundImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal];
    //    [self.searchButton addTarget:self action:@selector(onClickSearchBtn) forControlEvents:UIControlEventTouchUpInside];
    //    self.navigationItem.titleView = self.searchButton;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView==self.tableView) {
        NSLog(@"%@",@"外层tableView");
    }else{
        NSLog(@"%@",@"neicengtableView");
    }
    
    
    
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY < -IMAGE_HEIGHT) {
        [self updateNavBarButtonItemsAlphaAnimated:.0f];
    } else {
        [self updateNavBarButtonItemsAlphaAnimated:1.0f];
    }
    
    if (offsetY > NAVBAR_COLORCHANGE_POINT)
    {
        CGFloat alpha = (offsetY - NAVBAR_COLORCHANGE_POINT) / NAV_HEIGHT;
        [self wr_setNavBarBackgroundAlpha:alpha];
        [self updateSearchBarColor:alpha];
    }
    else
    {
        [self wr_setNavBarBackgroundAlpha:0];
        [self.searchButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    }
    
    //限制下拉的距离
    if(offsetY < LIMIT_OFFSET_Y) {
        [scrollView setContentOffset:CGPointMake(0, LIMIT_OFFSET_Y)];
    }
}

- (void)updateNavBarButtonItemsAlphaAnimated:(CGFloat)alpha
{
    [UIView animateWithDuration:0.2 animations:^{
        [self.navigationController.navigationBar wr_setBarButtonItemsAlpha:alpha hasSystemBackIndicator:NO];
    }];
}

- (void)updateSearchBarColor:(CGFloat)alpha
{
    UIColor *color = [[UIColor colorWithRed:29/255.0 green:160/255.0 blue:126/255.0 alpha:1.0] colorWithAlphaComponent:alpha];
    UIImage *image = [UIImage imageNamed:@"search"];
    image = [image wr_updateImageWithTintColor:color alpha:alpha];
    [self.searchButton setBackgroundImage:image forState:UIControlStateNormal];
    [self.searchButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}






-(void)setupUI
{
    [self wr_setNavBarBarTintColor:[UIColor colorWithRed:28/255.0 green:178/255.0 blue:140/255.0 alpha:1.0]];
    [self wr_setNavBarBackgroundAlpha:0];
    [self setupNavItems];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WYRecommendCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WYRecommendCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"WYRecommendCell" owner:nil options:nil].firstObject;
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld---%ld",indexPath.section,indexPath.row);
}









/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
